package com.zeny.coding.dto;


import com.zeny.coding.util.DtoUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import javax.xml.bind.ValidationException;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChapterDto {

    private String id;

    private String courseId;

    private String name;

    private Integer sort;


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", courseId=").append(courseId);
        sb.append(", name=").append(name);
        sb.append("]");
        return sb.toString();
    }

    /**
     * <h2>检查dto参数是否非法</h2>
     *
     * @param
     **/
    public void isValidForSave() throws ValidationException {
        valid();
        if (!StringUtils.isEmpty(id)) {
            id = null;
        }
    }

    /**
     * <h2>检查dto参数是否非法</h2>
     *
     * @param
     **/
    public void isValidForUpdate() throws ValidationException {
        //检查id
        DtoUtil.valid(this.id, true, 1, 19);
        valid();
    }

    private void valid() throws ValidationException {
        //检查大章名字
        DtoUtil.valid(this.name, true, 1, 50);
        //检查课程
        DtoUtil.valid(this.courseId, true, 1, 19);
        //检查排序
        DtoUtil.require(this.sort);
    }

}